package com.yunquan.ohana.ui.history.preview.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.github.chrisbanes.photoview.PhotoView
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack
import com.yunquan.ohana.R
import com.yunquan.ohana.entity.LocalFileEntity
import com.yunquan.ohana.utils.StringUtils
import com.yunquan.ohana.view.SampleCoverVideo
import java.io.File

class HistoryPreviewAdapter(
    private val dataList: List<LocalFileEntity>
) : RecyclerView.Adapter<HistoryPreviewAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryPreviewAdapter.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_preview, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: HistoryPreviewAdapter.ViewHolder, position: Int) {
        val index = position % dataList.size
        val item = dataList[index]
        if (item.type == 1) {
            holder.showImage(item)
        } else {
            holder.playVideo(item)
        }
        holder.setContent(item)
    }


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivPic: PhotoView = itemView.findViewById(R.id.iv_content)
        private val videoView: SampleCoverVideo = itemView.findViewById(R.id.video_content)
        private val tvContent: TextView = itemView.findViewById(R.id.tv_content)

        fun setContent(item: LocalFileEntity) {
            if (!StringUtils.isEmpty(item.content)) {
                tvContent.visibility = View.VISIBLE
                tvContent.text = item.content
            } else {
                tvContent.visibility = View.GONE
            }
        }

        fun showImage(item: LocalFileEntity) {
            ivPic.visibility = View.VISIBLE
            var isShow = true
            if (!StringUtils.isEmpty(item.localCropPath)) {
                val file = File(item.localCropPath!!)
                if (file.exists()) {
                    isShow = false
                    Glide.with(itemView.context).load(file).into(ivPic)
                }
            }
            if (isShow && !StringUtils.isEmpty(item.localFilePath)) {
                val file = File(item.localFilePath!!)
                if (file.exists()) {
                    isShow = false
                    Glide.with(itemView.context).load(file).into(ivPic)
                }
            }
            if (isShow && !StringUtils.isEmpty(item.fileUrl)) {
                Glide.with(itemView.context).load(item.fileUrl).into(ivPic)
            }
        }

        fun playVideo(item: LocalFileEntity) {
            videoView.visibility = View.VISIBLE
            // 背景图
            val thumbImageView = ImageView(itemView.context)
            if (item.videoImgUrl != null) {
                Glide.with(itemView.context).load(item.videoImgUrl).into(thumbImageView)
            } else if (item.thumbnail != null) {
                val file = File(item.thumbnail!!)
                if (file.exists()) {
                    Glide.with(itemView.context).load(file).into(thumbImageView)
                }
            }
            videoView.thumbImageView = thumbImageView
            // 查询文件
            var videoPath: String? = null
            if (!StringUtils.isEmpty(item.localCropPath)) {
                val file = File(item.localCropPath!!)
                if (file.exists()) {
                    videoPath = item.localCropPath
                }
            } else if (!StringUtils.isEmpty(item.localFilePath)) {
                val file = File(item.localFilePath!!)
                if (file.exists()) {
                    videoPath = item.localFilePath
                }
            } else if (!StringUtils.isEmpty(item.fileUrl)) {
                videoPath = item.fileUrl
            }
            // 加载视频
            GSYVideoOptionBuilder().setUrl(videoPath)
                .setCacheWithPlay(false)
                .setLooping(false)
                .setAutoFullWithSize(false)
                .setShowFullAnimation(true)
                .setReleaseWhenLossAudio(false)
                .setThumbPlay(true)
                .setNeedLockFull(false)
                .setIsTouchWigetFull(true)
                .setIsTouchWiget(true)
                .setHideKey(true)
                .build(videoView)
            videoView.titleTextView.visibility = View.GONE
            videoView.backButton.visibility = View.GONE
            videoView.fullscreenButton.setOnClickListener {
                videoView.startWindowFullscreen(itemView.context, false, false)
            }
            videoView.setVideoAllCallBack(object : VideoAllCallBack {

                override fun onPrepared(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.GONE
                }

                override fun onAutoComplete(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.VISIBLE
                }

                override fun onPlayError(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.VISIBLE
                }

                override fun onClickStop(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.VISIBLE
                }

                override fun onStartPrepared(url: String?, vararg objects: Any?) {}

                override fun onClickStartIcon(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.GONE
                }

                override fun onClickStartError(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.VISIBLE
                }

                override fun onClickStopFullscreen(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.VISIBLE
                }

                override fun onClickResume(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.GONE
                }

                override fun onClickResumeFullscreen(url: String?, vararg objects: Any?) {
                    tvContent.visibility = View.GONE
                }

                override fun onClickSeekbar(url: String?, vararg objects: Any?) {}

                override fun onClickSeekbarFullscreen(url: String?, vararg objects: Any?) {}

                override fun onComplete(url: String?, vararg objects: Any?) {}

                override fun onEnterFullscreen(url: String?, vararg objects: Any?) {}

                override fun onQuitFullscreen(url: String?, vararg objects: Any?) {}

                override fun onQuitSmallWidget(url: String?, vararg objects: Any?) {}

                override fun onEnterSmallWidget(url: String?, vararg objects: Any?) {}

                override fun onTouchScreenSeekVolume(url: String?, vararg objects: Any?) {}

                override fun onTouchScreenSeekPosition(url: String?, vararg objects: Any?) {}

                override fun onTouchScreenSeekLight(url: String?, vararg objects: Any?) {}

                override fun onClickStartThumb(url: String?, vararg objects: Any?) {}

                override fun onClickBlank(url: String?, vararg objects: Any?) {}

                override fun onClickBlankFullscreen(url: String?, vararg objects: Any?) {}

            })
        }
    }

}